<?php

	class pageContent {
		
		/**
		 *
		 * @var String get param action
		 */
		protected $action;
		
		/**
		 *
		 * @var ADOConnection database connection
		 */
		protected $db;
		
		/**
		 *
		 * @var Smarty smarty instance pointer
		 */
		protected $smarty;
		
		public function __construct($action, $db, &$smarty) {
			$this->action = $action;
			$this->db = &$db;
			$this->smarty = $smarty;
		}
		
		protected function getTopLinks() {
			
			$sql = <<<SQL
				SELECT
					action,
					text,
					order_number,
					target
				FROM
					menu_links
				WHERE
					area = 'top'
				AND
					order_number != 0
				AND
					active = 1
				ORDER BY
					order_number
				ASC
				;
SQL;
			return (array)$this->db->GetAll($sql);
			
		}
		
		protected function getMenuLinks() {
			
			$sql = <<<SQL
				SELECT
					action,
					text
				FROM
					menu_links
				WHERE
					area = 'menu'
				AND
					order_number != 0
				AND
					active = 1
				ORDER BY
					order_number
				ASC
				;
SQL;
			return (array)$this->db->GetAll($sql);
			
		}
		
		protected function getBoxes() {
			
			$sql = <<<SQL
				SELECT
					name,
					title
				FROM
					boxes
				WHERE
					order_number != 0
				AND
					active = 1
				ORDER BY
					order_number
				ASC
				;
SQL;
			return (array)$this->db->GetAll($sql);
			
		}
		
		protected function getActionContent() {
			
			$sql = <<<SQL
				SELECT
					ml.content_source,
					ac.content
				FROM
					menu_links ml
					LEFT JOIN
						action_content ac
						ON
							(ml.action = ac.action)
				WHERE
					ml.action = {$this->db->qstr($this->action)}
				;
SQL;
			$content = (array)$this->db->GetRow($sql);
			
			if (empty($content['content_source'])) {		# action not found
				return '<h1>Fehler!</h1><p>Der gew&uuml;nschte Bereich konnte nicht ge&ouml;ffnet werden!</p>';
			}
			
			if ($content['content_source'] == 'db') {		# db stored content
				$output = $content['content'];
			} elseif ($content['content_source'] == 'template') {		# get content from smarty template
				$output = $this->smarty->fetch('content/' . $this->action . '.tpl');
			}
			
			return $output;
			
		}
		
		public function render() {
			$this->smarty->assign('top_links', $this->getTopLinks());
			$this->smarty->assign('menu_links', $this->getMenuLinks());
			$this->smarty->assign('boxes', $this->getBoxes());
			$this->smarty->assign('action_content', $this->getActionContent());
			
			$this->smarty->display('index.html');
		}
		
	}

?>
